//
//  GuideUserInfoViewController.m
//  BaseProject
//
//  Created by 王洋 on 2021/4/12.
//  Copyright © 2021 王洋. All rights reserved.
//
#import "UserInfoRequest.h"
#import "GuideUserInfoViewController.h"

@interface GuideUserInfoViewController ()

@property (weak, nonatomic) IBOutlet UIButton *cancleBtn;

@property (weak, nonatomic) IBOutlet UIButton *handleBtn;

@property (weak, nonatomic) IBOutlet UIView *bgView;

@property (weak, nonatomic) IBOutlet UIImageView *userIcon;
@property (weak, nonatomic) IBOutlet UIImageView *iconTag;

@property (weak, nonatomic) IBOutlet UITextField *nameTF;

@property (weak, nonatomic) IBOutlet UIButton *womanBtn;
@property (weak, nonatomic) IBOutlet UIButton *manBtn;

@property (nonatomic, strong) UserInfoModel *model;

@end

@implementation GuideUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_UserInfo];
}

- (void)initView {
    
    //屏幕常亮
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    self.rt_disableInteractivePop = NO;
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 40)];
    leftView.backgroundColor = [UIColor clearColor];
    
    self.nameTF.leftView =leftView;
    self.nameTF.leftViewMode = UITextFieldViewModeAlways;
    
    self.userIcon.userInteractionEnabled = YES;
    
    [self.womanBtn setBackgroundColor:UIColorHex(0xFF638E)];
    [self.manBtn setBackgroundColor:UIColorHex(0xC4EDFD)];
    self.model.sex = 2;
    
    WEAKSELF
    [self.userIcon addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {

        [[TT_OSSManager manager] selectPhotoFromVC:self photoCount:1 AndUploadServerComption:^(NSArray * _Nonnull urlArray) {
            
            if (urlArray.count > 0) {
                [weakSelf.userIcon sd_setImageWithURL:[NSURL URLWithString:urlArray.firstObject]];
                weakSelf.model.imgUrl = urlArray.firstObject;
            }
        }];
    }];
    
    [[RACSignal merge:@[self.nameTF.rac_textSignal, RACObserve(self.nameTF, text)]] subscribeNext:^(NSString* text){
        self.model.nickname = self.nameTF.text;
    }];
}

- (void)fillWithModel:(UserInfoModel *)model {
    
    if (model.imgUrl.length > 0) {
        UIImage *hold = [UIImage imageNamed:@"guide_usericon"];
        [self.userIcon sd_setImageWithURL:[NSURL URLWithString:model.imgUrl] placeholderImage:hold];
    } else {
        [self.userIcon setImage:[UIImage imageNamed:@"guide_usericon"]];
    }

    self.nameTF.text = model.nickname;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.cancleBtn pp_clipCorners:UIRectCornerAllCorners radius:self.cancleBtn.frame.size.height/2 border:1 color:UIColorHex(0xf7f7f7)];
        
        [self.bgView pp_clipCorners:UIRectCornerAllCorners radius:12];
        [self.manBtn pp_clipCorners:UIRectCornerAllCorners radius:8];
        [self.womanBtn pp_clipCorners:UIRectCornerAllCorners radius:8];
        [self.nameTF pp_clipCorners:UIRectCornerAllCorners radius:8];
        [self.handleBtn pp_clipCorners:UIRectCornerAllCorners radius:self.handleBtn.frame.size.height/2];
        [self.userIcon pp_clipCorners:UIRectCornerAllCorners radius:self.userIcon.frame.size.height/2];

    });
}

- (IBAction)cancleAction:(id)sender {
    if (self.completion) {
        self.completion(YES);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)handleAction:(id)sender {
    [self request_updateUserInfo];
}

- (IBAction)selWoman:(id)sender {
    self.model.sex = 2;
    
    [self.womanBtn setBackgroundColor:UIColorHex(0xFF638E)];
    [self.manBtn setBackgroundColor:UIColorHex(0xC4EDFD)];

}

- (IBAction)selMan:(id)sender {
    self.model.sex = 1;
    [self.womanBtn setBackgroundColor:UIColorHex(0xFFDBE5)];
    [self.manBtn setBackgroundColor:UIColorHex(0x2BBFF7)];
}

- (UserInfoModel *)model {
    if (!_model) {
        _model = [[UserInfoModel alloc] init];
    }
    return _model;
}

- (void)request_UserInfo {
    WEAKSELF
    [UserInfoRequest request_userInfoComption:^(BOOL success, UserInfoModel * _Nonnull model) {
        if (success) {
            weakSelf.model = model;
            [weakSelf fillWithModel:weakSelf.model];
        }
    }];
}

- (void)request_updateUserInfo {
    NSMutableDictionary *dic = @{}.mutableCopy;
    self.model.nickname = [Utilities disable_emoji:self.model.nickname];
    dic = [self.model modelToJSONObject];
    
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_EditMyInfo];
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                [UserInfoRequest request_userInfoComption:^(BOOL success, UserInfoModel * _Nonnull model) {
                    if (success) {
                        if (weakSelf.completion) {
                            weakSelf.completion(YES);
                        }
                        [weakSelf.navigationController popViewControllerAnimated:YES];
                        
                    }
                }];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

@end
